package com.vanzo.contact;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.vanzo.bean.Contact;
import com.vanzo.talkie2.R;
import com.vanzo.talkie2.TextUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tisen on 2018/1/17.
 */

public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactItem> {

    private Context context;
    private List<Contact> contacts = new ArrayList<>();
    private ItemOnClickListener itemOnClickListener;

    public ContactsAdapter(Context context, List<Contact> contacts) {
        this.context = context;
        this.contacts = contacts;
    }

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public void setContacts(List<Contact> contacts) {
        this.contacts = contacts;
        notifyDataSetChanged();
    }

    @Override
    public ContactItem onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ContactItem(LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ContactItem holder, final int position) {
        final Contact contact = getItem(position);
        holder.getName().setText(contact.getName());
        holder.getHeader().setText(contact.getPinyin().substring(0, 1).toUpperCase());
        if (position == 0) {
            holder.getHeader().setVisibility(View.VISIBLE);
        } else {
            if (!TextUtils.equals(contact.getPinyin().substring(0, 1), contacts.get(position - 1).getPinyin().substring(0, 1))) {
                holder.getHeader().setVisibility(View.VISIBLE);
            } else {
                holder.getHeader().setVisibility(View.GONE);
            }
        }
        holder.getName().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemOnClickListener != null) {
                    itemOnClickListener.onClick(contact.getId());
                }
            }
        });
        holder.getName().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (itemOnClickListener != null) {
                    itemOnClickListener.onLongClick(contact.getId());
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return contacts.size();
    }

    public Contact getItem(int position) {
        if (position < 0 || position > contacts.size() - 1) {
            return null;
        }
        return contacts.get(position);
    }

    class ContactItem extends RecyclerView.ViewHolder {

        private TextView header;
        private TextView name;

        public ContactItem(View itemView) {
            super(itemView);
            header = (TextView) itemView.findViewById(R.id.contact_item_header);
            name = (TextView) itemView.findViewById(R.id.contact_item_name);
        }

        public TextView getHeader() {
            return header;
        }

        public TextView getName() {
            return name;
        }
    }

    interface ItemOnClickListener {
        void onClick(Long id);

        void onLongClick(Long id);
    }
}
